Skip to content

[8주차/엠버] 워크북 제출합니다.#79

Open
haewonwon wants to merge 4 commits into
UMC-Inha:엠버/mainfrom
haewonwon:week8
Open

[8주차/엠버] 워크북 제출합니다.#79
haewonwon wants to merge 4 commits into
UMC-Inha:엠버/mainfrom
haewonwon:week8

Conversation

@haewonwon
Copy link
Copy Markdown

@haewonwon haewonwon commented May 20, 2026

✅ 워크북 체크리스트

  • 모든 핵심 키워드 정리를 마쳤나요?
  • 핵심 키워드에 대해 완벽히 이해하셨나요?
  • 이론 학습 이후 직접 실습을 해보는 시간을 가졌나요?
  • 미션을 수행하셨나요?
  • 미션을 기록하셨나요?

✅ 컨벤션 체크리스트

  • 디렉토리 구조 컨벤션을 잘 지켰나요?
  • pr 제목을 컨벤션에 맞게 작성하였나요?
  • pr에 해당되는 이슈를 연결하였나요?(중요)
  • 적절한 라벨을 설정하였나요?
  • 파트장에게 code review를 요청하기 위해 reviewer를 등록하였나요?
  • 닉네임/main 브랜치의 최신 상태를 반영하고 있는지 확인했나요?(매우 중요!)

📌 주안점

@haewonwon haewonwon requested a review from qkrdmsthff as a code owner May 20, 2026 06:05
@haewonwon haewonwon changed the title keyword: 8주차 키워드 정리 [8주차/엠버] 워크북 제출합니다. May 20, 2026
@haewonwon haewonwon linked an issue May 20, 2026 that may be closed by this pull request
* - ESC 키 입력 시 자동으로 닫힘
* - 사이드바가 열리면 body 스크롤을 막아 배경 콘텐츠가 스크롤되지 않음
*/
function useSidebar() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엠버! 사이드바 열고 닫는 코드가 진짜 깔끔해서 딱 봤을 때 이해가 잘 되는 것 같아요! 나중에 엠버코드를 참고해서 다시 한 번 만들어 봐야겠어요!

Comment on lines +1 to +28
import { useCallback, useRef } from 'react';

/**
* 지정한 interval 동안 최대 한 번만 실행되는 스로틀된 함수를 반환함
* - fn은 ref로 관리하여 interval이 바뀌지 않아도 항상 최신 콜백이 호출됨
* - 언마운트 시 타이머를 clearTimeout으로 정리함
*/
function useThrottle<T extends unknown[]>(
fn: (...args: T) => void,
interval: number,
): (...args: T) => void {
const lastTimeRef = useRef(0);
const fnRef = useRef(fn);
fnRef.current = fn;

return useCallback(
(...args: T) => {
const now = Date.now();
if (now - lastTimeRef.current >= interval) {
lastTimeRef.current = now;
fnRef.current(...args);
}
},
[interval],
);
}

export default useThrottle;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 값(value)을 throttle하는 방식으로 구현했는데, 엠버는 함수(fn)를 직접 throttle하는 방식으로 구현했네요fnRef.current = fn 패턴으로 항상 최신 콜백을 참조하는 부분이 인상적입니다 ! !

현재 구현은 interval 안에 들어온 마지막 이벤트가 완전히 무시되는 것 같은데, 저는 setTimeout으로 남은 시간 후에 trailing 실행을 해줬습니다. 사용 목적에 따라 다를 것 같지만 만일 마지막 이벤트도 반영되어야 한다면 trailing 처리를 고려해보면 좋을 것 같아요!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chapter08_Debouncing & Throttling

3 participants